//package com.per.mybootall.es;
//
//
//import com.alibaba.fastjson.JSONObject;
//
//import com.alipay.api.domain.Person;
//import com.sun.scenario.Settings;
//import org.elasticsearch.transport.client.PreBuiltTransportClient;
//import org.junit.Test;
//
//
//import javax.swing.*;
//import java.io.IOException;
//import java.net.InetAddress;
//import java.net.UnknownHostException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ExecutionException;
//
//
//
//
//public class ElkTest {
//    private TransportClient client = null;
//
//    @BeforeTest
//    public void test1() throws UnknownHostException {
//        Settings settings = Settings.builder().put("cluster.name", "myes").build();
//
//        TransportAddress address1 = new TransportAddress(InetAddress.getByName("node01"), 9300);
//        TransportAddress address2 = new TransportAddress(InetAddress.getByName("node02"), 9300);
//        TransportAddress address3 = new TransportAddress(InetAddress.getByName("node02"), 9300);
//        client = new PreBuiltTransportClient(settings)
//                .addTransportAddress(address1)
//                .addTransportAddress(address2)
//                .addTransportAddress(address3);
//
//        System.out.println(client.toString());
//    }
//
//    /**
//     * 自己拼装json创建索引保存到myindex1索引库下面的article当中去
//     */
//    /**
//     * 插入json格式的索引数据
//     */
//    @Test
//    public void createIndex(){
//        String json = "{" +
//                "\"user\":\"kimchy\"," +
//                "\"postDate\":\"2020-01-30\"," +
//                "\"message\":\"travelying out Elasticsearch\"" +
//                "}";
//        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("myindex1", "article", "1").setSource(json, XContentType.JSON);
//
//        //调用get方法触发请求真正的去执行
//        IndexResponse indexResponse = indexRequestBuilder.get();
//    }
//
//    /*
//    使用map创建索引
//     */
//    @Test
//    public void createIndex2() {
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("name", "zhangsan");
//        map.put("age", "20");
//        map.put("sex", "0");
//
//        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("myindex1", "article", "2").setSource(map);
//        //触发请求真正的去执行
//        indexRequestBuilder.get();
//    }
//
//    /**
//     * 第三种：
//     * XcontentBuilder实现创建索引
//     */
//    @Test
//    public  void createIndex3() throws IOException {
//
//        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("myindex1", "article", "3").setSource(new XContentFactory().jsonBuilder()
//                .startObject()
//                .field("name", "李四")
//                .field("age", "55")
//                .field("address", "北京").endObject());
//
//        //触发请求真正的去执行
//        IndexResponse indexResponse = indexRequestBuilder.get();
//    }
//
//    @Test
//    public void objToIndex(){
//        Person person = new Person();
//        person.setAge(18);
//        person.setId(20);
//        person.setName("张三丰");
//        person.setAddress("武当山");
//        person.setEmail("zhangsanfeng@163.com");
//        person.setPhone("18588888888");
//        person.setSex(1);
//        String json = JSONObject.toJSONString(person);
//        System.out.println(json);
//        client.prepareIndex("myindex1","article","32").setSource(json,XContentType.JSON).get();
//        client.close();
//    }
//
//    /**
//     * 批量创建索引
//     * @throws IOException
//     */
//    @Test
//    public void index4() throws IOException {
//        BulkRequestBuilder bulk = client.prepareBulk();
//        bulk.add(client.prepareIndex("myindex1", "article", "7")
//                .setSource(new XContentFactory().jsonBuilder()
//                        .startObject()
//                        .field("name", "wangwu")
//                        .field("age", "18")
//                        .field("sex", "0")
//                        .field("address", "bj")
//                        .endObject()));
//        bulk.add(client.prepareIndex("news", "article", "8")
//                .setSource(new XContentFactory().jsonBuilder()
//                        .startObject()
//                        .field("name", "zhaoliu")
//                        .field("age", "18")
//                        .field("sex", "0")
//                        .field("address", "bj")
//                        .endObject()));
//        BulkResponse bulkResponse = bulk.get();
//        System.out.println(bulkResponse);
//        client.close();
//    }
//
//
//    /**
//     * 更新索引操作
//     */
//    @Test
//    public void updateIndex(){
//        HashMap<String, String> map = new HashMap<>();
//        map.put("phone", "15899996666");
//
//        UpdateResponse updateResponse = client.prepareUpdate("myindex1", "article", "7").setDoc(map).get();
//
//    }
//
//    /**
//     * 删除索引
//     */
//    @Test
//    public void deleteIndex(){
//        client.prepareDelete("myindex1","article","7").get();
//    }
//
//    /**
//     * 删除整个索引库
//     */
//    @Test
//    public void deleteIndexDB(){
//        client.admin().indices().prepareDelete("myindex1").execute().actionGet();
//    }
//
//
//    /**
//     * 初始化一批数据到索引库当中去准备做查询使用
//     * 注意这里初始化的时候，需要给我们的数据设置分词属性
//     * @throws Exception
//     */
//    @Test
//    public void createIndexBatch() throws Exception {
//        Settings settings = Settings
//                .builder()
//                .put("cluster.name", "myes") //节点名称， 在es配置的时候设置
//                //自动发现我们其他的es的服务器
//                .put("client.transport.sniff", "true")
//                .build();
//        //创建客户端
//        TransportClient client = new PreBuiltTransportClient(settings)
//                .addTransportAddress(new TransportAddress(InetAddress.getByName("node01"), 9300));//以本机作为节点
//        //创建映射
//        XContentBuilder mapping = jsonBuilder()
//                .startObject()
//                .startObject("properties")
//                //      .startObject("m_id").field("type","keyword").endObject()
//                .startObject("id").field("type", "integer").endObject()
//                .startObject("name").field("type", "text").field("analyzer", "ik_max_word").endObject()
//                .startObject("age").field("type", "integer").endObject()
//                .startObject("sex").field("type", "text").field("analyzer", "ik_max_word").endObject()
//                .startObject("address").field("type", "text").field("analyzer", "ik_max_word").endObject()
//                .startObject("phone").field("type", "text").endObject()
//                .startObject("email").field("type", "text").endObject()
//                .startObject("say").field("type", "text").field("analyzer", "ik_max_word").endObject()
//                .endObject()
//                .endObject();
//        //pois：索引名   cxyword：类型名（可以自己定义）
//        PutMappingRequest putmap = Requests.putMappingRequest("indexsearch").type("mysearch").source(mapping);
//        //创建索引
//        client.admin().indices().prepareCreate("indexsearch").execute().actionGet();
//        //为索引添加映射
//        client.admin().indices().putMapping(putmap).actionGet();
//
//
//        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
//        Person lujunyi = new Person(2, "玉麒麟卢俊义", 28, 1, "水泊梁山", "17666666666", "lujunyi@itcast.com","hello world今天天气还不错");
//        Person wuyong = new Person(3, "智多星吴用", 45, 1, "水泊梁山", "17666666666", "wuyong@itcast.com","行走四方，抱打不平");
//        Person gongsunsheng = new Person(4, "入云龙公孙胜", 30, 1, "水泊梁山", "17666666666", "gongsunsheng@itcast.com","走一个");
//        Person guansheng = new Person(5, "大刀关胜", 42, 1, "水泊梁山", "17666666666", "wusong@itcast.com","我的大刀已经饥渴难耐");
//        Person linchong = new Person(6, "豹子头林冲", 18, 1, "水泊梁山", "17666666666", "linchong@itcast.com","梁山好汉");
//        Person qinming = new Person(7, "霹雳火秦明", 28, 1, "水泊梁山", "17666666666", "qinming@itcast.com","不太了解");
//        Person huyanzhuo = new Person(8, "双鞭呼延灼", 25, 1, "水泊梁山", "17666666666", "huyanzhuo@itcast.com","不是很熟悉");
//        Person huarong = new Person(9, "小李广花荣", 50, 1, "水泊梁山", "17666666666", "huarong@itcast.com","打酱油的");
//        Person chaijin = new Person(10, "小旋风柴进", 32, 1, "水泊梁山", "17666666666", "chaijin@itcast.com","吓唬人的");
//        Person zhisheng = new Person(13, "花和尚鲁智深", 15, 1, "水泊梁山", "17666666666", "luzhisheng@itcast.com","倒拔杨垂柳");
//        Person wusong = new Person(14, "行者武松", 28, 1, "水泊梁山", "17666666666", "wusong@itcast.com","二营长。。。。。。");
//
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "1")
//                .setSource(JSONObject.toJSONString(lujunyi), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "2")
//                .setSource(JSONObject.toJSONString(wuyong), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "3")
//                .setSource(JSONObject.toJSONString(gongsunsheng), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "4")
//                .setSource(JSONObject.toJSONString(guansheng), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "5")
//                .setSource(JSONObject.toJSONString(linchong), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "6")
//                .setSource(JSONObject.toJSONString(qinming), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "7")
//                .setSource(JSONObject.toJSONString(huyanzhuo), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "8")
//                .setSource(JSONObject.toJSONString(huarong), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "9")
//                .setSource(JSONObject.toJSONString(chaijin), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "10")
//                .setSource(JSONObject.toJSONString(zhisheng), XContentType.JSON)
//        );
//        bulkRequestBuilder.add(client.prepareIndex("indexsearch", "mysearch", "11")
//                .setSource(JSONObject.toJSONString(wusong), XContentType.JSON)
//        );
//
//        bulkRequestBuilder.get();
//        client.close();
//
//    }
//
//    /**
//     * 通过每条数据的系统id来进行查询
//     */
//    @Test
//    public void getBySystemId(){
//        //通过系统id来进行查询，调用prepareGet方法，传入三个参数
//        GetRequestBuilder getRequestBuilder = client.prepareGet("indexsearch", "mysearch", "11");
//        //获取到查询的数据结果
//        GetResponse getResponse = getRequestBuilder.get();
//        String id = getResponse.getId();//获取系统id为多少
//        System.out.println("系统id为"+ id);
//        String sourceAsString = getResponse.getSourceAsString();//将结果转换成为json格式的字符串
//        System.out.println(sourceAsString);
//    }
//
//    /**
//     * 查询索引库当中所有的数据
//     */
//    @Test
//    public void queryAll(){
//        SearchResponse searchResponse = client
//                .prepareSearch("indexsearch")//指定我们查询的索引库
//                .setTypes("mysearch")  //指定我们需要查询哪个类型
//                .setQuery(QueryBuilders.matchAllQuery())  //指定我们查询的条件
//                .get();  //调用get方法，触发我们的请求真正的去执行
//
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hitsAll = hits.getHits();
//        //每一个searchHit封装完了我们的一条数据
//        for (SearchHit searchHit : hitsAll) {
//
//            String id = searchHit.getId();
//            System.out.println("系统id为" +  id);
//            String sourceAsString = searchHit.getSourceAsString();
//            System.out.println(sourceAsString);
//        }
//
//    }
//
//    /**
//     * 年龄范围值查询
//     * 查询年龄是18到28的数据
//     */
//    @Test
//    public void rangeQuery(){
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.rangeQuery("age").gt(18).lte(28)).get();
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hitsAll = hits.getHits();
//        for (SearchHit searchHit : hitsAll) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//    }
//
//    /**
//     * 按照词条进行查询
//     */
//    @Test
//    public void termQuery(){
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.termQuery("say", "熟悉")).get();
//
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//    }
//
//    /**
//     * 模糊查询，自动纠正拼写错误单词，最大纠正次数是两次
//     * 查询  say   helol
//     */
//    @Test
//    public void fuzzyQuery(){
//        //通过模糊查询，查询helol这个单词
//        FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("say", "helol").fuzziness(Fuzziness.TWO);
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(fuzziness).get();
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//    }
//
//    /**
//     * 通配符查询
//     *  *  表示匹配任意多个字符
//     *  ？  表示匹配一个字符
//     */
//    @Test
//    public void  wildCardQuery(){
//        WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("say", "hell?");
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(wildcardQuery).get();
//
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//
//    }
//
//    /**
//     * 使用booleanQuery实现多条件组合查询
//     * 查询年龄是18到28范围内且性别是男性的，第一个条件  里面包含两部分  年龄和性别  且的关系
//     * 或者id范围在10到13范围内的  第二个条件
//     * 第一个条件与第二个条件是一个或的关系
//     */
//    @Test
//    public void booleanQuery(){
//        //年龄范围的条件
//        RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gt(17).lt(29);
//        //性别的条件
//        TermQueryBuilder sex = QueryBuilders.termQuery("sex", "1");
//        //数据id的条件
//        RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gt("9").lt("14");
//        //使用booleanQuery组合我们多个查询条件
//        BoolQueryBuilder should = QueryBuilders.boolQuery()
//                .should(id).should(QueryBuilders.boolQuery().must(sex).must(age));
//
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(
//                should
//        ).get();
//
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//    }
//
//    /**
//     * 分页查询
//     */
//    @Test
//    public void pageQuery(){
//
//        int pageSize  = 5 ;
//        int pageNum =2;
//
//        //计算起始的数据
//        int startNum = (pageNum - 1 ) * pageSize;
//
//        SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.matchAllQuery())
//                .addSort("id", SortOrder.ASC)
//                .setFrom(startNum)
//                .setSize(pageSize)
//                .get();
//
//        SearchHits hits = searchResponse.getHits();
//        SearchHit[] hits1 = hits.getHits();
//        for (SearchHit searchHit : hits1) {
//            System.out.println(searchHit.getId());
//            System.out.println(searchHit.getSourceAsString());
//        }
//    }
//
//    /**
//     * 高亮显示，对我们查询出来的say   hello
//     * 这个字段进行高亮显示
//     */
//    @Test
//    public void highLighter(){
//
//        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.termQuery("say", "hello"));
//        //通过HighlightBuilder 来定义我们对哪个字段进行高亮显示，以及设置高亮的前缀和后缀
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        highlightBuilder.field("say").preTags("<font style='color:red'>").postTags("</font>");
//        //通过SearchRequestBuilder 来设置高亮显示
//        SearchResponse searchResponse = searchRequestBuilder.highlighter(highlightBuilder).get();
//        //获取高亮结果
//        SearchHits hits = searchResponse.getHits();
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//            Text[] says = hit.getHighlightFields().get("say").getFragments();
//            for (Text say : says) {
//                System.out.println(say);
//            }
//        }
//    }
//
//
//    /**
//     * 批量添加数据
//     * @throws IOException
//     * @throws ExecutionException
//     * @throws InterruptedException
//     */
//    @Test
//    public void addIndexDatas() throws IOException, ExecutionException, InterruptedException {
//        //获取settings
//        //配置es集群的名字
//        Settings settings = Settings.builder().put("cluster.name", "myes").build();
//        //获取客户端
//        TransportAddress transportAddress = new TransportAddress(InetAddress.getByName("node01"), 9300);
//
//        TransportAddress transportAddress2 = new TransportAddress(InetAddress.getByName("node02"), 9300);
//
//        TransportAddress transportAddress3 = new TransportAddress(InetAddress.getByName("node03"), 9300);
//        //获取client客户端
//        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress).addTransportAddress(transportAddress2).addTransportAddress(transportAddress3);
//
//        /**
//         * 创建索引
//         * */
//        client.admin().indices().prepareCreate("player").get();
//        //构建json的数据格式，创建映射
//        XContentBuilder mappingBuilder = jsonBuilder()
//                .startObject()
//                .startObject("player")
//                .startObject("properties")
//                .startObject("name").field("type","text").field("index", "true").field("fielddata","true").endObject()
//                .startObject("age").field("type","integer").endObject()
//                .startObject("salary").field("type","integer").endObject()
//                .startObject("team").field("type","text").field("index", "true").field("fielddata","true").endObject()
//                .startObject("position").field("type","text").field("index", "true").field("fielddata","true").endObject()
//                .endObject()
//                .endObject()
//                .endObject();
//        PutMappingRequest request = Requests.putMappingRequest("player")
//                .type("player")
//                .source(mappingBuilder);
//        client.admin().indices().putMapping(request).get();
//
//
//        //批量添加数据开始
//
//        BulkRequestBuilder bulkRequest = client.prepareBulk();
//
//// either use client#prepare, or use Requests# to directly build index/delete requests
//        bulkRequest.add(client.prepareIndex("player", "player", "1")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "郭德纲")
//                        .field("age", 33)
//                        .field("salary",3000)
//                        .field("team" , "cav")
//                        .field("position" , "sf")
//                        .endObject()
//                )
//        );
//        bulkRequest.add(client.prepareIndex("player", "player", "2")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "于谦")
//                        .field("age", 25)
//                        .field("salary",2000)
//                        .field("team" , "cav")
//                        .field("position" , "pg")
//                        .endObject()
//                )
//        );
//        bulkRequest.add(client.prepareIndex("player", "player", "3")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "岳云鹏")
//                        .field("age", 29)
//                        .field("salary",1000)
//                        .field("team" , "war")
//                        .field("position" , "pg")
//                        .endObject()
//                )
//        );
//
//        bulkRequest.add(client.prepareIndex("player", "player", "4")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "爱因斯坦")
//                        .field("age", 21)
//                        .field("salary",300)
//                        .field("team" , "tim")
//                        .field("position" , "sg")
//                        .endObject()
//                )
//        );
//
//        bulkRequest.add(client.prepareIndex("player", "player", "5")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "张云雷")
//                        .field("age", 26)
//                        .field("salary",2000)
//                        .field("team" , "war")
//                        .field("position" , "pf")
//                        .endObject()
//                )
//        );
//        bulkRequest.add(client.prepareIndex("player", "player", "6")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "爱迪生")
//                        .field("age", 40)
//                        .field("salary",1000)
//                        .field("team" , "tim")
//                        .field("position" , "pf")
//                        .endObject()
//                )
//        );
//        bulkRequest.add(client.prepareIndex("player", "player", "7")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "牛顿")
//                        .field("age", 21)
//                        .field("salary",500)
//                        .field("team" , "tim")
//                        .field("position" , "c")
//                        .endObject()
//                )
//        );
//
//        bulkRequest.add(client.prepareIndex("player", "player", "8")
//                .setSource(jsonBuilder()
//                        .startObject()
//                        .field("name", "特斯拉")
//                        .field("age", 20)
//                        .field("salary",500)
//                        .field("team" , "tim")
//                        .field("position" , "sf")
//                        .endObject()
//                )
//        );
//        BulkResponse bulkResponse = bulkRequest.get();
//        client.close();
//    }
//
//
//    /**
//     * 统计每个球队当中有多少人
//     */
//    @Test
//    public void countByTeam(){
//        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");
//
//        //设置我们按照哪个字段进行分组
//        TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//
//        searchRequestBuilder.addAggregation(team);
//        //调用get方法，触发请求真正的去执行
//        SearchResponse searchResponse = searchRequestBuilder.get();
//
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            //将aggregation进行强转，转换成为 StringTerms
//            StringTerms stringTerms = (StringTerms) aggregation;
//
//            List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
//
//            for (StringTerms.Bucket bucket : buckets) {
//                System.out.println(bucket.getKey());//获取聚合别名字段
//                System.out.println(bucket.getDocCount());//统计每个球队当中一共有多少个球员
//            }
//
//
//        }
//
//
//    }
//
//    /**
//     * 统计每个球队当中每个位置有多少人
//     */
//
//    @Test
//    public  void countByTeamAndPosition(){
//
//        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");
//
//        //设置我们team的聚合条件
//        TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//
//        //设置我们位置的聚合条件
//        TermsAggregationBuilder position = AggregationBuilders.terms("position_count").field("position");
//
//        //关联两个聚合条件
//        team.subAggregation(position);
//
//        //调用get方法，触发请求真正的去执行
//        SearchResponse searchResponse = searchRequestBuilder.addAggregation(team).get();
//
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            StringTerms stringTerms = (StringTerms) aggregation;
//
//            System.out.println("队伍聚合数据为" +  stringTerms.toString());
//
//
//            List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
//            for (StringTerms.Bucket bucket : buckets) {
//                //获取我们聚合的数据一共有多少条
//                long docCount = bucket.getDocCount();
//                System.out.println(docCount);
//
//                //获取数据的key
//                Object key = bucket.getKey();
//                System.out.println("当前队伍名称为" +  key+"当前队伍当中一共有" +  docCount + "人");
//
//                //求每个队伍当中，每个位置有多少人
//                Aggregation position_count = bucket.getAggregations().get("position_count");
//
//                StringTerms positionTerm = (StringTerms) position_count;
//                System.out.println("按照位置进行分组 " +  positionTerm.toString());
//
//                List<StringTerms.Bucket> buckets1 = positionTerm.getBuckets();
//                for (StringTerms.Bucket bucket1 : buckets1) {
//                    Object key1 = bucket1.getKey();
//                    long docCount1 = bucket1.getDocCount();//获取数据一共有多少条
//                    System.out.println("该队伍下面位置为" +  key1 + "有" +  docCount1+"人");
//                }
//            }
//        }
//    }
//
//    /**
//     * 计算每个球队年龄最大值
//     */
//    @Test
//    public  void groupAndMax(){
//        SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//        TermsAggregationBuilder team = AggregationBuilders.terms("team_group").field("team");
//
//        MaxAggregationBuilder age = AggregationBuilders.max("max_age").field("age");
//
//        team.subAggregation(age);
//        SearchResponse searchResponse = builder.addAggregation(team).get();
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            StringTerms stringTerms = (StringTerms) aggregation;
//            List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
//            for (StringTerms.Bucket bucket : buckets) {
//                Aggregation max_age = bucket.getAggregations().get("max_age");
//                System.out.println(max_age.toString());
//            }
//        }
//    }
//
//    /**
//     * 统计每个球队中年龄最小值
//     */
//    @Test
//    public  void  teamMinAge(){
//
//        SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//
//        TermsAggregationBuilder team = AggregationBuilders.terms("team_count").field("team");
//
//        MinAggregationBuilder age = AggregationBuilders.min("min_age").field("age");
//
//        TermsAggregationBuilder termAggregation = team.subAggregation(age);
//
//        SearchResponse searchResponse = builder.addAggregation(termAggregation).get();
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            System.out.println(aggregation.toString());
//
//            StringTerms stringTerms = (StringTerms) aggregation;
//            List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
//            for (StringTerms.Bucket bucket : buckets) {
//                Aggregations aggregations1 = bucket.getAggregations();
//                for (Aggregation aggregation1 : aggregations1) {
//                    System.out.println(aggregation1.toString());
//                }
//            }
//        }
//    }
//
//    /**
//     * 计算每个球队的年龄平均值
//     */
//    @Test
//    public void avgTeamAge(){
//        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");
//
//        //按照球队进行分组
//        TermsAggregationBuilder team = AggregationBuilders.terms("team_group").field("team");
//
//        AvgAggregationBuilder age = AggregationBuilders.avg("avg_age").field("age");
//
//
//        //求取每个球队当中年年龄最小值
//        // MinAggregationBuilder age = AggregationBuilders.min("min_age").field("age");
//
//        //对age字段取最大值
//        //  MaxAggregationBuilder age = AggregationBuilders.max("max_age").field("age");
//        //设置分组以及求最大值的关系
//        team.subAggregation(age);
//
//        SearchResponse searchResponse = searchRequestBuilder.addAggregation(team).get();
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            StringTerms stringTerms = (StringTerms) aggregation;
//            System.out.println(stringTerms.toString());
//
//            List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
//            //遍历每一个bucket，然后对数据获取最大的年龄值
//            for (StringTerms.Bucket bucket : buckets) {
//                //求每个球队当中年龄最大值
//                // Aggregation max_age = bucket.getAggregations().get("max_age");
//                //求每个球队当中年龄最小值
//                //Aggregation min_age = bucket.getAggregations().get("min_age");
//
//
//                //求每个球队当中年龄的平均值
//                Aggregation avg_age = bucket.getAggregations().get("avg_age");
//
//
//
//                System.out.println(avg_age.toString());
//            }
//
//
//        }
//
//    }
//
//    /**
//     * 统计每个球队当中的球员平均年龄，以及队员总年薪
//     */
//    @Test
//    public void avgAndSum(){
//        SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//
//        TermsAggregationBuilder team_group = AggregationBuilders.terms("team_group").field("team");
//
//        AvgAggregationBuilder avg_age = AggregationBuilders.avg("avg_age").field("age");
//
//        SumAggregationBuilder sumMoney = AggregationBuilders.sum("sum_money").field("salary");
//
//
//        TermsAggregationBuilder termsAggregationBuilder = team_group.subAggregation(avg_age).subAggregation(sumMoney);
//
//        SearchResponse searchResponse = builder.addAggregation(termsAggregationBuilder).get();
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            System.out.println(aggregation.toString());
//        }
//
//
//    }
//
//
//    /**
//     * 聚合排序
//     * 计算每个球队的总年薪，然后按照年薪进行排序
//     */
//    @Test
//    public void groupAndCount(){
//
//        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");
//
//        //按照球队进行分组
//        TermsAggregationBuilder teamGroup = AggregationBuilders.terms("team_group").field("team").order(BucketOrder.count(true));
//        //获取每个球队的年薪的总和
//        SumAggregationBuilder sumSalary = AggregationBuilders.sum("sum_salary").field("salary");
//
//        teamGroup.subAggregation(sumSalary);
//        //调用get方法，触发请求真正的去执行
//        SearchResponse searchResponse = searchRequestBuilder.addAggregation(teamGroup).get();
//        //获取我们的数据的聚合值
//        Aggregations aggregations = searchResponse.getAggregations();
//        for (Aggregation aggregation : aggregations) {
//            System.out.println(aggregation.toString());
//        }
//
//
//    }
//
//    /**
//     * 关闭连接
//     */
//    @AfterTest
//    public void closeClient() {
//        client.close();
//    }
//
//
//
//
//
//
//
//
//}
